<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
* Klasa Object_attributes controller obavlja validaciju podataka i komunicira sa modelom.
* 
* Vraca podatke korisnikckoj aplikaciji.
* @author NU SIT Bar
* @version 1.0
* @created 15-Aug-2012 1:41:14 PM
*/
class Object_attributes extends CI_Controller
{

    /**
    * Attribute type
    */
    var $type;
    /**
    * post_action, can be NEW || EDIT
    */
    var $action;

    function __construct(){
        parent::__construct();
        $this->load->model('codegen_model');
    }



    /**
    * Return view of all attributes for given type
    * 
    * @param post_type
    */
    function view_all($post_type)
    {
    }

    /**
    * 
    * @param type
    */
    function view_edit($type=NULL)
    {
        $this->data['page'] = 'attributes';
        $this->data['title'] = 'Uredi atribute';
        $this->data['type'] = $type;

        $q = 'SELECT oa.ID, oa.type, oa.custom, a.ID as att_ID, a.name, a.value, a.required, a.custom
        FROM object_attributes as oa
        LEFT JOIN attributes as a ON oa.attributes_ID = a.ID
        WHERE oa.type = "'.$type.'"
        ORDER BY oa.order ASC, oa.custom ASC, oa.ID ASC 
        ';

        $this->data['object_attributes'] = $this->db->query($q)->result_array();

        echo $this->load->view('object_attributes/edit_object_attributes',$this->data,TRUE);
    }

    /**
    * 
    * @param type
    */
    function view_new($type=NULL)
    {
        $this->data['page'] = 'attributes';
        $this->data['title'] = 'Novi atribut objektu';
        $this->data['type'] = $type;
        //$this->data['attributes'] = $this->codegen_model->get('attributes','*','',999999);
        $this->data['attributes'] = $this->codegen_model->get('attributes','*',"custom = 1",999999);
        echo $this->load->view('object_attributes/new_object_attribute',$this->data,TRUE);

    }

    /**
    * 
    * @param post_action
    * @param post_ID
    */
    function submit_post()
    {

        if ($this->form_validation->run('object_attributes') == false)
        {
            echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
        } 
        else
        {                            

            $record['type'] = $_POST['attrToObjectDialog_type'];
            $record['custom'] = $_POST['attrToObjectDialog_custom'];
            $record['attributes_ID'] = $_POST['attrToObjectDialog_attributes_ID'];

            if ($this->codegen_model->add('object_attributes',$record) == TRUE)
            {
                echo json_encode(array('action'=>true,'msg'=>'Successful insert operation.'));
            }
            else
            {
                echo json_encode(array('action'=>FALSE, 'msg'=>'<p>An Error Occured.</p>')); 

            }
        }

    }

    function reorder_object_attributes()
    {
        $record['type'] = $_POST['editAttrToObjectDialog_type'];
        unset($_POST['editAttrToObjectDialog_type']);

        $cnt = 0;
        foreach ($_POST as $value):
            $id = explode('_',$value);
            $this->db->where('ID',$id[1]);
            $this->db->update('object_attributes',array('order'=>$cnt));
            $cnt++;
            endforeach;
    }

    function delete($ID){
        $this->db->where('ID',$ID);
        $this->db->delete('object_attributes');
        echo json_encode(array('action'=>true));

    }

    function dropdown_check($str)
    {
        $CI =& get_instance();

        $CI->form_validation->set_message('dropdown_check', 'Please select %s.');

        return ($str=='0') ? FALSE : TRUE;
    }


}
